package nu.aing.P20140908;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author memekmaut@gmail.com
 */
public class Egalitarianism3Easy {
    
    public static void main(String[] args) {
        Egalitarianism3Easy obj = new Egalitarianism3Easy();
        System.out.println(obj.maxCities(
                4, 
                new int[] {1,1,1},
                new int[] {2,3,4},
                new int[] {1,1,1}
        ));
        System.out.println(obj.maxCities(
                6, 
                new int[] {1,2,3,2,3},
                new int[] {2,3,4,5,6},
                new int[] {2,1,3,2,3}
        ));
        System.out.println(obj.maxCities(
                10, 
                new int[] {1,1,1,1,1,1,1,1,1},
                new int[] {2,3,4,5,6,7,8,9,10},
                new int[] {1000,1000,1000,1000,1000,1000,1000,1000,1000}
        ));
        System.out.println(obj.maxCities(
                2, 
                new int[] {1},
                new int[] {2},
                new int[] {3}
        ));
        System.out.println(obj.maxCities(
                1, 
                new int[] {},
                new int[] {},
                new int[] {}
        ));
    }
    
    /**
     * @param n - cities count.
     * @param a - cities
     * @param b - cities [a] connected to
     * @param len - road length
     */
    public int maxCities(int n, int[] a, int[] b, int[] len) {
        List<Wrap> ws = new LinkedList<Egalitarianism3Easy.Wrap>();
        for(int i = 0; i < n-1; i++) {
            ws.add(new Wrap(a[i], b[i], len[i]));
        }
        
        Map<Integer, List<Integer>> result = new LinkedHashMap<Integer, List<Integer>>();
        for(int i = 0; i < ws.size(); i++) {
            Wrap w = ws.get(i);
            int count = w.len;
            int cityCount = 1;
            int cp = w.b;
            for(int j = 0; j < ws.size(); j++) {
                if(i == j) continue;
                if(cp == ws.get(j).a) {
                    count += w.len;
                    cityCount++;
                    cp = ws.get(j).b;
                }
            }
            if(!result.containsKey(count) ) {
                result.put(count, new LinkedList<Integer>() );
            }
            result.get(count).add(cityCount);
        }
        
        for(List<Integer> x : result.values() ) {
            int t = 0;
            for(Integer c : x ) {
                t+= c;
            }
            return t;
        }
        return 1;
    }
    
    class Wrap {
        final int a;
        final int b;
        final int len;
        public Wrap(int a, int b, int len) {
            this.a = a;
            this.b = b;
            this.len = len;
        }
        
        @Override
        public String toString() {
            return "a:"+a+",b:"+b+",l:"+len;
        }
    }
}
